import { ref, onMounted } from "vue";
import {
  findCategoryChildListByName,
  getOrganizersList as getOrganizersListAPI,
} from "@/api/contest";
import { organizerType } from "@/types/api/coutest";

import { categoryType } from "@/types/publishContest";
import { ElMessage } from "element-plus";
import { useRoute } from "vue-router";
import useUserStore from "@/stores/user";
export function usePublish() {
  const route = useRoute();
  const categoryList = ref<categoryType[]>();
  const userStore = useUserStore()
  const upHeaders = ref({
    token: "",
  });
  const optionProps = ref({
    value: "id",
    label: "categoryName",
    children: "children",
})
  const organizersData = ref<organizerType[]>([]);

  // 获取发布比赛的分类列表
  async function getPublishCategoryList(): Promise<categoryType[] | undefined> {
    const name = (route.name as string).substring(2);
    let res = await findCategoryChildListByName(name);
    if (res.data.code != 200) {
      ElMessage.error(res.data.msg);console
    } else {
      return res.data.data.reverse();
    }
  }
  // 获取主办方列表
  async function getOrganizersList(): Promise<organizerType[]> {
    const res = await getOrganizersListAPI();
    return (res.data as any).data;
  }
  onMounted(async () => {
    categoryList.value = await getPublishCategoryList();
    // 获取用户信息
    upHeaders.value.token = userStore.userInfo.token;
    organizersData.value = await getOrganizersList();
  });
  return {
    categoryList,
    upHeaders,
    organizersData,
    optionProps
  };
}
